/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.util;

import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.SimpleExpression;
import org.hibernate.criterion.Criterion;



/**
 * Tipos de operadores disponibles.<br>
 * Los valores fueron tomados de la doc de Hibernate.<br>
 * Cuando es LIKE tiene que ser un {@link String} el value
 * 
 * @author nicolas.gonzalez
 */
public enum ExpressionOperator {
    
    Equals("="),
    NotEquals("<>"),
    Like(" like "),
    ILike(" ilike "),
    GreaterThan(">"),
    GreaterEqualsThan(">="),
    LowerThan("<"),
    LowerEqualsThan("<=");
    

    private String operatorAsStr;
    
    ExpressionOperator(String operatorAsStr) {
        this.operatorAsStr = operatorAsStr;
    }

    /**
     * @return the operatorAsStr
     */
    public String getOperatorAsStr() {
        return this.operatorAsStr;
    }
    
    /**
     * Devuelve un {@link SimpleExpression} de acuerdo
     * al operador.<br>
     * @param property
     * @param value
     * @return Un {@link SimpleExpression}
     * @see SimpleExpression
     */
    public Criterion asSimpleExpression(String property, Object value) {
        if (this.operatorAsStr.equals(Equals.getOperatorAsStr())) {
            return Expression.eq(property, value);
        }
        else if (this.operatorAsStr.equals(NotEquals.getOperatorAsStr())) {
            return Expression.ne(property, value);
        }
        else if (this.operatorAsStr.equals(Like.getOperatorAsStr())) {
            return Expression.like(property, (String)value, MatchMode.ANYWHERE);
        }
        else if (this.operatorAsStr.equals(ILike.getOperatorAsStr())) {
            return Expression.ilike(property, (String)value, MatchMode.ANYWHERE);
        }
        else if (this.operatorAsStr.equals(GreaterThan.getOperatorAsStr())) {
            return Expression.gt(property, value);
        }
        else if (this.operatorAsStr.equals(GreaterEqualsThan.getOperatorAsStr())) {
            return Expression.ge(property, value);
        }
        else if (this.operatorAsStr.equals(LowerThan.getOperatorAsStr())) {
            return Expression.lt(property, value);
        }
        else if (this.operatorAsStr.equals(LowerEqualsThan.getOperatorAsStr())) {
            return Expression.le(property, value);
        }
        // Nunca deberia entrar aca
        return null;
    }

    /**
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        return this.operatorAsStr;
    }

    
}
